package com.iwechat.config;

import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.druid.util.DruidPasswordCallback;
import com.iwechat.security.SecurityUtil;

/** 数据库密码回调解密 */
public class DBPasswordCallback extends DruidPasswordCallback {
    private static final long   serialVersionUID = 1L;
    private static final byte[] key              = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };

    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        String pwd = properties.getProperty("password");
        if (StringUtils.isNotBlank(pwd)) {
            try {
                String password = SecurityUtil.decryptDes(pwd, key);
                setPassword(password.toCharArray());
            } catch (Exception e) {
                setPassword(pwd.toCharArray());
            }
        }
    }

    public static void main(String[] args) {
        // TODO 请使用该方法加密后，把密文写入classpath:/config/jdbc.properties
        System.out.println(SecurityUtil.encryptDes("hell", key));
    }
}
